package day03;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import work.JDBCUtils;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class demo {
    public static void main(String[] args) throws PropertyVetoException {
        // 需求：遍历exam的用户。
        // 首先创建c3p0的数据库连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();
//设置c3p0数据库连接池需要的参数
//        cpds.setDriverClass("com.mysql.jdbc.Driver");
//        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/my02");
//        cpds.setUser("root");
//        cpds.setPassword("1234");

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            // 从连接池中获取连接
            conn = cpds.getConnection();
            // 获得发送sql的对象
            String sql = "select * from exam";
            stmt = conn.prepareStatement(sql);
            // 执行sql 获得结果
            rs = stmt.executeQuery();
            // 处理结果
            while (rs.next()) {
                //取出数据
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String chinese = rs.getString("chinese");
                //输出
                System.out.println(id + "==" + name + "=" + chinese);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源: 关闭stmt 和 rs
            JDBCUtils.release(conn, stmt, rs);
        }
    }
}
